<template>
  <div>
    <el-container class="container_home">
      <el-header class="header_home">
        <div class="header_left">
          <img src="../assets/logo.png" alt="" />
          <span>电商后台管理</span>
        </div>
        <el-button type="info" @click="logout">LogOut</el-button>
      </el-header>
      <el-container>
        <el-aside
          class="aside_home"
          :width="isCollapse != true ? '200px' : '64px'"
        >
          <div class="toggle" @click="toggle">|||</div>
          <!-- 设置menu背景颜色 
           -->
          <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409eff"
            :unique-opened="true"
            ref="menu"
            :collapse="isCollapse"
            :collapse-transition="false"
            router
            :default-active="active_index"
          >
            <el-submenu index="1">
              <!-- 一级菜单模板区域 -->
              <template slot="title">
                <!-- 图片 -->
                <i class="el-icon-location"></i>
                <!-- 文字 -->
                <span>导航一</span>
              </template>
              <!-- 二级菜单     -->
              <el-menu-item index="/welcome" >
                <!-- 图片 -->
                <i class="el-icon-location"></i>
                <!-- 文字 -->
                <span>菜单一</span>
              </el-menu-item>
              <!-- 二级菜单     -->
              <el-menu-item index="/welcome2" >
                <!-- 图片 -->
                <i class="el-icon-location"></i>
                <!-- 文字 -->
                <span>菜单二</span>
              </el-menu-item>
            </el-submenu>

            <el-submenu index="2">
              <!-- 一级菜单模板区域 -->
              <template slot="title">
                <!-- 图片 -->
                <i class="el-icon-location"></i>
                <!-- 文字 -->
                <span style="color: #fff">导航一</span>
              </template>
              <!-- 二级菜单     -->
              <el-menu-item index="2-1">
                <!-- 图片 -->
                <i class="el-icon-location"></i>
                <!-- 文字 -->
                <span style="color: #fff">导航一</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main class="main_home">
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isCollapse: false,
      active_index:'/welcome',
    };
  },
  created(){
       this.active_index = this.$route.path
       window.addEventListener('popstate', this.popstate, false);
      
        // let ac_index = sessionStorage.getItem('active_index')
        // if(null != ac_index)
        // this.active_index = sessionStorage.getItem('active_index')
  },
  watch:{
    //    path:{//深度监听，可监听到对象、数组的变化
    //      handler(val, oldVal){
    //        this.active_index = this.path
    //      },
    //      deep:true //true 深度监听
    //  }
  },
  methods: {
    logout() {
      this.$http.get("http://localhost:8088/login").then((res) => {
        //清空token
        sessionStorage.clear();
        //跳转首页
        this.$router.push("/login");
      });
    },
    toggle() {
      //拿到menu对象 修改属性
      this.isCollapse = !this.isCollapse;
    },
    // testClick(e){
    //     this.active_index = e.index
    //     sessionStorage.setItem('active_index',e.index)
    // }
    popstate(){
         this.active_index = this.$route.path
    }
  },
};
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
  .header_home {
    display: flex;
    justify-content: space-between;
    background-color: #373d41;
    align-items: center;

    .header_left {
      display: flex;
      color: #fff;
      font-size: 20px;
      align-items: center;

      img {
        width: 40px;
        height: 40px;
      }
    }
  }
  .aside_home {
    background-color: #333744;
    .el-menu {
      border-right: 0px;
    }
    .toggle {
      color: #fff;
      height: 20px;
      text-align: center;
      font-size: 6px;
      background-color: #4a5064;
      letter-spacing: 0.2em;
      cursor: pointer;
    }
  }
  .main_home {
    background-color: #eaedf1;
  }
}

div {
  height: 100%;
}
</style>

